/*
 * Item.java
 *
 * Created on January 5, 2008, 1:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Item {
    private dbConnection dbcon = new dbConnection();
    private String itemName, itemType, description, rarity;
    private int iLevel, sellPrice, buyPrice, atk, matk, def, res, mob, str, dex, iint, vit, spr, range;
    private double crit, dodge, block, acc, hpr, mpr, spd, hp, mp, mhp, mmp;
    /** Creates a new instance of Item */
    public Item() {
    }
    
    public Item(String iname, String itype, String idescription, String irarity, int ilevel, int isellPrice, int ibuyPrice, int iatk, int imatk, int idef, int ires, double icrit,double idodge,double iblock,double iacc,double ihpr,double impr, int imob, int istr, int idex, int itint, int ivit, int ispr, int irange, double ihp, double imp, double imhp, double immp)
    {
        setItemName(iname);
        setItemType(itype);
        setDescription(idescription);
        setRarity(irarity);
        setILevel(ilevel);
        setSellPrice(isellPrice);
        setBuyPrice(ibuyPrice);
        setAtk(iatk);
        setMatk(imatk);
        setDef(idef);
        setRes(ires);
        setCrit(icrit);
        setDodge(idodge);
        setBlock(iblock);
        setAcc(iacc);
        setHpr(ihpr);
        setMpr(impr);
        setMob(imob);
        setStr(istr);
        setDex(idex);
        setIint(itint);
        setVit(ivit);
        setSpr(ispr);
        setRange(irange);
        setMmp(immp);
        setMhp(imhp);
        setMp(imp);
        setHp(ihp);
    }
    
    public boolean checkExistance (String iName) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM Item WHERE itemName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, iName);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    
    public void addItem (Item i){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        if (!checkExistance(i.getItemName()))
        {
            getDbcon().connect();
            query = "INSERT INTO Item (itemName, itemType, description, rarity, iLevel, sellPrice, buyPrice, atk, matk, def, res, crit, dodge, block, acc, spd, hpr, mpr, isEquiped, mobility, str, dex, ints, vit, spr, range, maxMP, maxHP, mp, hp) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, i.itemName);
                stmt.setString(2, i.itemType);
                stmt.setString(3, i.description);
                stmt.setString(4, i.rarity);
                stmt.setInt(5, i.iLevel);
                stmt.setInt(6, i.sellPrice);
                stmt.setInt(7, i.buyPrice);
                stmt.setInt(8, i.atk);
                stmt.setInt(9, i.matk);
                stmt.setInt(10, i.def);
                stmt.setInt(11, i.res);
                stmt.setDouble(12, i.crit);
                stmt.setDouble(13, i.dodge);
                stmt.setDouble(14, i.block);
                stmt.setDouble(15, i.acc);
                stmt.setDouble(16, i.spd);
                stmt.setDouble(17,i.getHpr());
                stmt.setDouble(18,i.getMpr());
                stmt.setInt(19, i.mob);
                stmt.setInt(20, i.str);
                stmt.setInt(21, i.dex);
                stmt.setInt(22, i.iint);
                stmt.setInt(23, i.vit);
                stmt.setInt(24, i.spr);
                stmt.setInt(25, i.range);
                stmt.setDouble(26, i.mmp);
                stmt.setDouble(27, i.mhp);
                stmt.setDouble(28,i.getMp());
                stmt.setDouble(29,i.getHp());
                
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();
        }
    }
    
     public void deleteItem (String iName){
         		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE Item WHERE itemName = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, iName);
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
     }
      public void viewItem (String iName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT itemName, itemType, description, rarity, iLevel, sellPrice, buyPrice, atk, matk, def, res, crit, dodge, block, acc, spd, hpr, mpr, mobility, str, dex, ints, vit, spr, range, maxMP, maxHP, mp, hp FROM Item WHERE itemName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, iName);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                itemName = rs.getString("itemName").trim();
                itemType = rs.getString("itemType").trim();
                description = rs.getString("description").trim();
                rarity = rs.getString("rarity").trim();
                iLevel = rs.getInt("iLevel");
                sellPrice = rs.getInt("sellPrice");
                buyPrice = rs.getInt("buyPrice");
                atk = rs.getInt("atk");
                matk = rs.getInt("matk");
                def = rs.getInt("def");
                res = rs.getInt("res");
                crit = rs.getDouble("crit");
                dodge = rs.getDouble("dodge");
                block = rs.getDouble("block");
                acc = rs.getDouble("acc");
                spd = rs.getDouble("spd");
                setHpr(rs.getDouble("hpr"));
                setMpr(rs.getDouble("mpr"));
                mob = rs.getInt("mobility");
                str = rs.getInt("str");
                dex = rs.getInt("dex");
                iint = rs.getInt("ints");
                vit = rs.getInt("vit");
                spr = rs.getInt("spr");
                range = rs.getInt("range");
                mmp = rs.getDouble("maxMP");
                mhp = rs.getDouble("maxHP");
                setMp(rs.getDouble("mp"));
                setHp(rs.getDouble("hp"));
             }
            //crset.populate(rs);
            rs.close(); 					// close the result
            stmt.close();
           // return crset; // close the statement					// close the statement
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
        }
     }
    
      public Vector viewItemsbyType (String type){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Item> iList;
            
        iList = new Vector<Item>();
        Iterator it = iList.iterator();
                
        getDbcon().connect();
        query = "SELECT itemName, itemType, description, rarity, iLevel, sellPrice, buyPrice, atk, matk, def, res, crit, dodge, block, acc, spd, hpr, mpr, mobility, str, dex, ints, vit, spr, range, maxMP, maxHP, mp, hp FROM Item WHERE itemType = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, type);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Item i = new Item();
                i.itemName = rs.getString("itemName").trim();
                i.itemType = rs.getString("itemType").trim();
                i.description = rs.getString("description").trim();
                i.rarity = rs.getString("rarity").trim();
                i.iLevel = rs.getInt("iLevel");
                i.sellPrice = rs.getInt("sellPrice");
                i.buyPrice = rs.getInt("buyPrice");
                i.atk = rs.getInt("atk");
                i.matk = rs.getInt("matk");
                i.def = rs.getInt("def");
                i.res = rs.getInt("res");
                i.crit = rs.getDouble("crit");
                i.dodge = rs.getDouble("dodge");
                i.block = rs.getDouble("block");
                i.acc = rs.getDouble("acc");
                i.spd = rs.getDouble("spd");
                i.setHpr(rs.getDouble("hpr"));
                i.setMpr(rs.getDouble("mpr"));
                i.mob = rs.getInt("mobility");
                i.str = rs.getInt("str");
                i.dex = rs.getInt("dex");
                i.iint = rs.getInt("ints");
                i.vit = rs.getInt("vit");
                i.spr = rs.getInt("spr");
                i.range = rs.getInt("range");
                i.mmp = rs.getDouble("maxMP");
                i.mhp = rs.getDouble("maxHP");
                i.setMp(rs.getDouble("mp"));
                i.setHp(rs.getDouble("hp"));
                iList.add(i);
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return iList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }

      public Vector viewCharItems (String cname){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Item> iList;
            
        iList = new Vector<Item>();
        Iterator it = iList.iterator();
                
        getDbcon().connect();
        query = "SELECT Item.itemName, Item.itemType, Item.description, Item.rarity, Item.iLevel, Item.sellPrice, Item.buyPrice, Item.atk, Item.matk, Item.def, Item.res, Item.crit, Item.dodge, Item.block, Item.acc, Item.spd, Item.hpr, Item.mpr, Item.mobility, Item.str, Item.dex, Item.ints, Item.vit, Item.spr, Item.range, Item.maxMP, Item.maxHP, Item.mp, Item.hp FROM Item, Knapsack WHERE Knapsack.itemName = Item.itemName AND Knapsack.characterName = ? AND Knapsack.isEquiped > 0";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Item i = new Item();
                i.itemName = rs.getString("itemName").trim();
                i.itemType = rs.getString("itemType").trim();
                i.description = rs.getString("description").trim();
                i.rarity = rs.getString("rarity").trim();
                i.iLevel = rs.getInt("iLevel");
                i.sellPrice = rs.getInt("sellPrice");
                i.buyPrice = rs.getInt("buyPrice");
                i.atk = rs.getInt("atk");
                i.matk = rs.getInt("matk");
                i.def = rs.getInt("def");
                i.res = rs.getInt("res");
                i.crit = rs.getDouble("crit");
                i.dodge = rs.getDouble("dodge");
                i.block = rs.getDouble("block");
                i.acc = rs.getDouble("acc");
                i.spd = rs.getDouble("spd");
                i.setHpr(rs.getDouble("hpr"));
                i.setMpr(rs.getDouble("mpr"));
                i.mob = rs.getInt("mobility");
                i.str = rs.getInt("str");
                i.dex = rs.getInt("dex");
                i.iint = rs.getInt("ints");
                i.vit = rs.getInt("vit");
                i.spr = rs.getInt("spr");
                i.range = rs.getInt("range");
                i.mmp = rs.getDouble("maxMP");
                i.mhp = rs.getDouble("maxHP");
                i.setMp(rs.getDouble("mp"));
                i.setHp(rs.getDouble("hp"));
                iList.add(i);
              
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return iList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemType() {
        return itemType;
    }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRarity() {
        return rarity;
    }

    public void setRarity(String rarity) {
        this.rarity = rarity;
    }

    public int getILevel() {
        return iLevel;
    }

    public void setILevel(int iLevel) {
        this.iLevel = iLevel;
    }

    public int getSellPrice() {
        return sellPrice;
    }

    public void setSellPrice(int sellPrice) {
        this.sellPrice = sellPrice;
    }

    public int getBuyPrice() {
        return buyPrice;
    }

    public void setBuyPrice(int buyPrice) {
        this.buyPrice = buyPrice;
    }

    public int getAtk() {
        return atk;
    }

    public void setAtk(int atk) {
        this.atk = atk;
    }

    public int getMatk() {
        return matk;
    }

    public void setMatk(int matk) {
        this.matk = matk;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }

    public int getRes() {
        return res;
    }

    public void setRes(int res) {
        this.res = res;
    }

    public double getCrit() {
        return crit;
    }

    public void setCrit(double crit) {
        this.crit = crit;
    }

    public double getDodge() {
        return dodge;
    }

    public void setDodge(double dodge) {
        this.dodge = dodge;
    }

    public double getBlock() {
        return block;
    }

    public void setBlock(double block) {
        this.block = block;
    }

    public double getAcc() {
        return acc;
    }

    public void setAcc(double acc) {
        this.acc = acc;
    }

    public double getHpr() {
        return hpr;
    }

    public void setHpr(double hpr) {
        this.hpr = hpr;
    }

    public double getMpr() {
        return mpr;
    }

    public void setMpr(double mpr) {
        this.mpr = mpr;
    }

    public int getMob() {
        return mob;
    }

    public void setMob(int mob) {
        this.mob = mob;
    }

    public double getSpd() {
        return spd;
    }

    public void setSpd(double spd) {
        this.spd = spd;
    }

    public int getStr() {
        return str;
    }

    public void setStr(int str) {
        this.str = str;
    }

    public int getDex() {
        return dex;
    }

    public void setDex(int dex) {
        this.dex = dex;
    }

    public int getIint() {
        return iint;
    }

    public void setIint(int iint) {
        this.iint = iint;
    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getSpr() {
        return spr;
    }

    public void setSpr(int spr) {
        this.spr = spr;
    }

    public int getRange() {
        return range;
    }

    public void setRange(int range) {
        this.range = range;
    }

    public double getMhp() {
        return mhp;
    }

    public void setMhp(double mhp) {
        this.mhp = mhp;
    }

    public double getMmp() {
        return mmp;
    }

    public void setMmp(double mmp) {
        this.mmp = mmp;
    }

    public double getHp() {
        return hp;
    }

    public void setHp(double hp) {
        this.hp = hp;
    }

    public double getMp() {
        return mp;
    }

    public void setMp(double mp) {
        this.mp = mp;
    }

}
